#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Predesys - Predesys LAN Radar
# Version: 0.2.2
# Author: Jose A. Jimenez <jajimc@gmail.com>
# License: GPL 3.0 (see "license" file)

import sys
sys.dont_write_bytecode = True
import radarexceptions

def get_formatted_message_list_header():
    return "<predesys_message_list>"
    
def get_formatted_message(destination, body):
    if destination == None or (type(destination) != str and type(destination) != unicode) or destination == "":
        raise radarexceptions.ValueNotValidException("'destination' must be a not null and not empty string")
    if body == None or (type(body) != str and type(body) != unicode) or body == "":
        raise radarexceptions.ValueNotValidException("'body' must be a not null and not empty string")
        
    return "<message><destination>%s</destination><body>%s</body></message>" % (destination, body)
    
def get_formatted_message_list_footer():
    return "</predesys_message_list>"
    
def is_message_list(messageListString):
    if messageListString == None or (type(messageListString) != str and type(messageListString) != unicode) or messageListString == "": return False
    
    header = get_formatted_message_list_header()
    footer = get_formatted_message_list_footer()
    return (messageListString[:len(header)] == header and messageListString[len(messageListString)-len(footer):] == footer)
    
def is_message_list_group(messageListGroupString):
    if messageListGroupString == None or (type(messageListGroupString) != str and type(messageListGroupString) != unicode) or messageListGroupString == "": return False
    
    messageListHeader = get_formatted_message_list_header()
    messageListFooter = get_formatted_message_list_footer()
    return (messageListGroupString[:len(messageListHeader)] == messageListHeader and messageListGroupString[len(messageListGroupString)-len(messageListFooter):] == messageListFooter)
    
def is_message(text):
    if text == None or (type(text) != str and type(text) != unicode) or text == "": return False
    return (text[:22] == "<message><destination>" and text[len(text)-17:] == "</body></message>")
  
def get_message_lists(messageListGroup):
    if not is_message_list_group(messageListGroup): raise radarexceptions.ValueNotValidException("'messageListGroup' must be valid message list group string")
    
    messageListHeader = get_formatted_message_list_header()
    messageListFooter = get_formatted_message_list_footer()
    
    found = True
    string = messageListGroup
    while found:
        begIndex = string.find(messageListHeader)
        endIndex = string.find(messageListFooter)
        found = (begIndex != -1 and endIndex != -1)
        if found:
            yield string[begIndex: endIndex + len(messageListFooter)]
            string = string[endIndex + len(messageListFooter) : ]
            
def get_messages(messageList):
    if not is_message_list(messageList): raise radarexceptions.ValueNotValidException("'messageList' must be valid message list string")
    
    messageHeader = "<message>"
    messageFooter = "</message>"
    
    found = False
    string = messageList
    while (not found):
        begIndex = string.find(messageHeader)
        endIndex = string.find(messageFooter)
        found = (begIndex != -1 and endIndex != -1)
        if found:
            yield string[begIndex: endIndex + len(messageFooter)]
            string = string[endIndex + len(messageFooter) : ]
            
def get_message_data(message):
    if not is_message(message): raise radarexceptions.ValueNotValidException("'message' must be valid message string")
    
    destinationHeader = "<destination>"
    destinationFooter = "</destination>"
    bodyHeader = "<body>"
    bodyFooter = "</body>"
    
    begIndex = message.find(destinationHeader)
    endIndex = message.find(destinationFooter)
        
    destination = message[begIndex + len(destinationHeader) : endIndex]
        
    begIndex = message.find(bodyHeader)
    endIndex = message.find(bodyFooter)
        
    body = message[begIndex + len(bodyHeader) : endIndex]
        
    return (destination, body)
